package pl.edu.pw.MidEmu.Samples.MidasChat.UI.Components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import midas.mw.interfaces.Mw;
import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Integration.MidEmuApplication;
import pl.edu.pw.MidEmu.Samples.MidasChat.Common.Consts;
import pl.edu.pw.MidEmu.Samples.MidasChat.UI.Controllers.MIDASChatFrameController;
import pl.edu.pw.MidEmu.Samples.MidasChat.UI.Views.MIDASChatFrameView;

/**
 * Class representing login frame
 * Performs operations associated with chat login
 * @author Mateusz Starodab
 *
 */
public class MIDASChatFrame extends BaseFrame<JFrame, MIDASChatFrameController> implements MIDASChatFrameView
{
	private JButton btnStartChat;
	private JLabel lblLocalContextClass;

	private Mw midas;

	private JPanel pnlMain;

	private JTextField tbxLocalContextClass;

	/**
	 * Default constructor
	 * @param middlewareInterface
	 */
	public MIDASChatFrame(Mw middlewareInterface)
	{
		midas = middlewareInterface;
		MIDASChatFrameController controller = new MIDASChatFrameController();
		controller.setView(this);
		setController(controller);
		JFrame frame = new JFrame();
		setComponent(frame);

		getComponent().setTitle(Consts.MIDASChatFrame_Title);
		getComponent().setSize(250, 120);
		getComponent().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getComponent().setLocationByPlatform(true);

		pnlMain = new JPanel(new BorderLayout());

		JPanel pnlCenter = new JPanel(new FlowLayout(FlowLayout.CENTER));

		tbxLocalContextClass = new JTextField(20);
		lblLocalContextClass = new JLabel(Consts.MIDASChatFrame_localContextClass);
		lblLocalContextClass.setLabelFor(tbxLocalContextClass);
		pnlCenter.add(lblLocalContextClass);
		pnlCenter.add(tbxLocalContextClass);

		JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.CENTER));
		btnStartChat = new JButton(Consts.MIDASChatFrame_btnStart);
		btnStartChat.setName(Consts.MIDASChatFrame_btnStart);
		btnStartChat.addActionListener(this.getController());
		pnlSouth.add(btnStartChat);

		pnlMain.add(pnlCenter, BorderLayout.CENTER);
		pnlMain.add(pnlSouth, BorderLayout.SOUTH);

		getComponent().setContentPane(pnlMain);
	}

	@Override
	/**
	 * Reports validation errors
	 */
	public void reportValidationError()
	{
		JOptionPane.showMessageDialog(this.getComponent(), "Midas chat configuration is invalid.\nMake sure you fill all necessary fields", "Invalid configuration", JOptionPane.ERROR_MESSAGE);
	}

	@Override
	/**
	 * Shows chat frame
	 */
	public void showChatFrame()
	{
		ChatFrame frame = new ChatFrame(MidEmuApplication.getInstance().getMidasLocalNodeId(), tbxLocalContextClass.getText(), midas);
		frame.init();
		frame.getComponent().setVisible(true);
		this.getComponent().setVisible(false);
		this.getComponent().dispose();
	}

	@Override
	/**
	 * Validates
	 */
	public boolean validate()
	{
		if (tbxLocalContextClass.getText().equals(""))
			return false;
		return true;
	}
}
